// pages/fenleishezhi/fenleishezhi.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false, //add弹窗
    feileiList: [], // 保存分类列表
    listindex: '', //为空添加，否则编辑
    values: '',
    numvalues:'',
    typeid: ''
  },
  // 手指按下
  handleTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  //手指离开
  handleTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  },
  changan(e) {
    let that = this;
    let listindex = e.currentTarget.dataset.listindex
    let lists = that.data.feileiList
    console.log(lists[listindex])
    wx.showModal({
      title: "提示",
      content: "是否删除该条分类",
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "upGoodsTypes",
              shopId: wx.getStorageSync('shopId'),
              name: lists[listindex].goodsTypeName,
              seq: lists[listindex].goodsTypeSeq,
              state: lists[listindex].goodsTypeState,
              type: 3,
              id: lists[listindex].goodsTypeId
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                lists.splice(listindex, 1)
                that.setData({
                  feileiList: lists
                })
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              }
            }
          });

        } else if (res.cancel) {}
      }
    });
    // that.gettongzhiList()
  },
  // 获取列表
  getlist() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          that.setData({
            feileiList: res.data.goodsTypes
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  // add弹窗
  addclass: function() {
    if (wx.getStorageSync('shopState') == 1) {
      wx.showToast({
        title: '店铺停用状态下，您无权进行此操作！',
        icon: 'none',
        duration: 2000
      })
      return
    }
    this.setData({
      showModal: true,
      values: ''
    })
  },
  // 编辑弹窗
  setclass: function(e) {
    console.log(e.currentTarget.dataset.listindex)
    let lists = this.data.feileiList
    this.setData({
      showModal: true,
      listindex: e.currentTarget.dataset.listindex,
      values: lists[e.currentTarget.dataset.listindex].goodsTypeName,
      typeid: e.currentTarget.dataset.typeid
    })
  },
  // 保存
  toBaocun() {
    let that = this
    if (!that.data.values) {
      wx.showToast({
        title: '分类设置不能为空',
        icon: 'none'
      })
      return
    }
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)

    let feileiList = that.data.feileiList
    let values = that.data.values
    let num = that.data.numvalues
    let types
    if (that.data.listindex === '') {
      types = 1
    } else {
      types = 2
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "upGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
        name: values,
        seq: num,
        state: 0,
        type: types,
        id: that.data.typeid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          that.getlist()
          that.setData({
            showModal: false,
            listindex: '',
            values: '',
            typeid: '',
            numvalues:''
          })
        }
      }
    });
  },
  close_mask: function() {
    this.setData({
      showModal: false,
      listindex: '',
      values: '',
      typeid: '',
      numvalues:''
    })
  },
  // 获取输入的值
  getvals(e) {
    console.log(e)
    this.setData({
      values: e.detail.value
    })
  },
  // 获取输入的值
  getvalnums(e) {
    console.log(e)
    this.setData({
      numvalues: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    let that = this
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})